﻿Imports DevExpress.XtraGrid.Views.Grid
Public Class frmAttribute
    Dim _mysql As New ClsSQLhelper
    Dim Strsql As String
    Dim Primary_Code As Integer = -1
    Dim on_load As Boolean = True
#Region "Function"
    Sub Load_Category()
        Try
            Strsql = "Select Cate_ID,Cate_Name from categories order by Cate_ID "
            Dim DT_Category As New DataTable
            DT_Category = _mysql.GetMYSQLDataTable(Strsql, "categories")
            With CboCategories
                .DataSource = DT_Category
                .DisplayMember = "Cate_Name"
                .ValueMember = "Cate_ID"
                .SelectedIndex = 0
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "OK_Button_Click"
                .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Section()
        Try

            Strsql = "Select Section_ID,Section_Name from sections where Cate_ID = " & Me.CboCategories.SelectedValue & " order by Section_ID "
            Dim DT_Section As New DataTable
            DT_Section = _mysql.GetMYSQLDataTable(strsql, "section")
            ' If DT_Section.Rows.Count = 0 Then Exit Sub
            With CboSection
                .DataSource = DT_Section
                .DisplayMember = "Section_Name"
                .ValueMember = "Section_ID"
                If DT_Section.Rows.Count > 0 Then
                    .SelectedIndex = 0

                Else
                    Me.CboSection.Text = ""

                End If
            End With

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "OK_Button_Click"
                .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Data(Optional ByVal StrCiteria As String = "")
        Try
            Strsql = "Select Attribute_ID,Attribute_Name,attribute_code,Section_Name,Cate_Name from attribute left join categories  on attribute.Cate_ID = categories.Cate_ID left join sections on attribute.section_id = sections.section_id"
            Strsql = Strsql & " where attribute.cate_ID = " & Me.CboCategories.SelectedValue & " and attribute.section_id = " & Me.CboSection.SelectedValue
            If StrCiteria <> "" Then
                Select Case SearchOption.EditValue
                    Case 0
                        Strsql = Strsql & " and Attribute_ID = " & StrCiteria

                    Case 1
                        Strsql = Strsql & " and Attribute_Name like '" & StrCiteria & "%'"
                End Select

            End If
            Strsql = Strsql & " order by attribute.Attribute_ID"
            Dim DT_Section As New DataTable
            DT_Section = _mysql.GetMYSQLDataTable(Strsql, "section")

            With GridAttribute
                .DataSource = DT_Section
                Me.GridColumn1.FieldName = "Attribute_ID"
                Me.GridColumn2.FieldName = "Attribute_Name"
                Me.GridColumn3.FieldName = "Cate_Name"
                Me.GridColumn4.FieldName = "attribute_code"
                Me.GridColumn5.FieldName = "Section_Name"
            End With
        Catch ex As Exception
            'Select Case (_mysql.MySQLExecuteScalar(Strsql))
            '    Case -1
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
                Exit Sub
            End With


        End Try

    End Sub
#End Region
    Private Sub frmAttribute_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Category()
        Load_Section()
        Load_Data()
        on_load = False
    End Sub

    Private Sub CboCategories_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboCategories.SelectedIndexChanged
        If on_load = True Then Exit Sub
        Load_Section()
        Load_Data()
    End Sub

    Private Sub MNAddSection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MNAddSection.Click

        If frmNewAttribute.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
        Load_Data()
    End Sub

    Private Sub MnEditSection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnEditSection.Click
        Try
            If Primary_Code = -1 Then Exit Sub
            Dim DT_Categories As New DataTable
            Strsql = "select attribute_ID,attribute_name,Section_ID,Cate_ID,attribute_code from attribute where attribute_ID = " & Primary_Code
            DT_Categories = _mysql.GetMYSQLDataTable(Strsql)
            With frmEditAttribute
                .TxtAttributeName.Text = DT_Categories.Rows(0).Item("attribute_name").ToString
                .TxtAttributeCode.Text = DT_Categories.Rows(0).Item("attribute_Code").ToString
                .Primary_Code = Primary_Code
                If .ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub

            End With
            Load_Data()
        Catch ex As Exception

            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "MnEditCategories_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With

        End Try
    End Sub

    Private Sub MnDeleteSection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnDeleteSection.Click
        Try
            If Primary_Code = -1 Then Exit Sub
            If (MsgBox("กรุณายืนยันการลบข้อมูล " & Primary_Code, MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
            If (MsgBox("การลบข้อมูลเมื่อยืนยันแล้วจะไม่สามารถนำกลับมาใหม่ได้ คุณยังต้องการยืนยันใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
            Strsql = "Select count(*) as cc from attribute where attribute_ID = " & Primary_Code
            If (_mysql.MySQLExecuteScalar(Strsql)) = 0 Then
                MsgBox("ไม่พบรหัสที่ต้องการลบ", MsgBoxStyle.Critical, "กรุณาตรวจสอบรายการลบ")
                Exit Sub
            End If


            Strsql = "Delete from attribute where  attribute_ID = " & Primary_Code

            Select Case (_mysql.MySQLExecuteScalar(Strsql))
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "MnDeleteSection_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                        Exit Sub
                    End With

                Case Else
                    MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการลบข้อมูล")


            End Select

            'Add Log
            Strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (10,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "','" & Primary_Code & "') "
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "MnDeleteCategories_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()

                    End With
            End Select

            Load_Data()
        Catch ex As Exception
            ' MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub CboSection_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboSection.SelectedIndexChanged
        If on_load = True Then Exit Sub
        Load_Data()
    End Sub

    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Try

            Dim Gv As GridView = sender
            Dim CodeName As String

            'Dim TypeCODE As String = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Item_Type")).ToString()
            If Me.SearchOption.EditValue = 0 Then
                CodeName = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Attribute_ID")).ToString()
            Else
                CodeName = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Attribute_Name")).ToString()
            End If
            Primary_Code = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Attribute_ID")).ToString()

            Me.TxtSearch.EditValue = CodeName
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub GridAttribute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridAttribute.Click

    End Sub
End Class